package com.gzw.shop.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gzw.shop.R;
import com.gzw.shop.bean.ShoppingCart;
import com.gzw.shop.bean.Wares;
import com.gzw.shop.utils.CartProvider;

import java.util.Iterator;
import java.util.List;

/**
 * Created by JACKYGUO on 2017-03-10.
 */

public class HWAdapter extends SimpleAdapter<Wares> {


    CartProvider mProvider;

    public HWAdapter(Context context, List<Wares> datas) {
        super(context, datas, R.layout.template_hot_wares);
        mProvider = new CartProvider(context);
    }

    @Override
    public void bindData(BaseViewHolder holder, final Wares wares) {


        SimpleDraweeView draweeView = (SimpleDraweeView) holder.getView(R.id.drawee_view);
        draweeView.setImageURI(Uri.parse(wares.getImgUrl()));

        holder.getTextView(R.id.text_title).setText(wares.getName());
        holder.getTextView(R.id.text_price).setText("￥" + wares.getPrice());

        Button button = holder.getButton(R.id.btn_add);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mProvider.put(wares);
                    Toast.makeText(mContext, "已添加到购物车", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

//    public ShoppingCart converData(Wares wares) {
//
//        ShoppingCart cart = new ShoppingCart();
//        cart.setId(wares.getId());
//        cart.setDescription(wares.getDescription());
//        cart.setImgUrl(wares.getImgUrl());
//        cart.setName(wares.getName());
//        cart.setPrice(wares.getPrice());
//        return cart;
//    }


    //重新设置加载的布局
    public void resetLayout(int layoutId) {
        this.mLayoutId = layoutId;
        notifyItemRangeChanged(0, getDatas().size());
    }

}
